package com.ruoyi.web.admin.controller.system;

import java.util.List;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.qo.HealthDataQo;
import com.ruoyi.system.domain.vo.HealthDataVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysHealthData;
import com.ruoyi.system.service.ISysHealthDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 健康数据Controller
 *
 * @author ruoyi
 */
@Api(tags = "健康数据管理")
@RestController
@RequestMapping("/system/healthData")
public class SysHealthDataController extends BaseController {
    private final ISysHealthDataService sysHealthDataService;

    public SysHealthDataController(ISysHealthDataService sysHealthDataService) {
        this.sysHealthDataService = sysHealthDataService;
    }

    /**
     * 查询健康数据列表
     */
    @ApiOperation("查询健康数据列表")
    @PreAuthorize("@ss.hasPermi('system:healthData:list')")
    @GetMapping("/list")
    public TableDataInfo list(HealthDataQo healthDataQo) {
        startPage();
        List<HealthDataVo> list = sysHealthDataService.selectHealthDataList(healthDataQo);
        return getDataTable(list);
    }

    /**
     * 获取健康数据详细信息
     */
    @ApiOperation("获取健康数据详细信息")
    @PreAuthorize("@ss.hasPermi('system:healthData:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam("健康数据ID") @PathVariable("id") Long id) {
        return success(sysHealthDataService.selectSysHealthDataById(id));
    }

    /**
     * 新增健康数据
     */
    @ApiOperation("新增健康数据")
    @PreAuthorize("@ss.hasPermi('system:healthData:add')")
    @Log(title = "健康数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysHealthData sysHealthData) {
        return toAjax(sysHealthDataService.insertSysHealthData(sysHealthData));
    }

    /**
     * 修改健康数据
     */
    @ApiOperation("修改健康数据")
    @PreAuthorize("@ss.hasPermi('system:healthData:edit')")
    @Log(title = "健康数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysHealthData sysHealthData) {
        return toAjax(sysHealthDataService.updateSysHealthData(sysHealthData));
    }

    /**
     * 删除健康数据
     */
    @ApiOperation("删除健康数据")
    @PreAuthorize("@ss.hasPermi('system:healthData:remove')")
    @Log(title = "健康数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam("健康数据ID数组") @PathVariable Long[] ids) {
        return toAjax(sysHealthDataService.deleteSysHealthDataByIds(ids));
    }

    /**
     * 根据设备ID查询最新健康数据
     */
    @ApiOperation("根据设备ID查询最新健康数据")
    @GetMapping("/latest/device/{deviceId}")
    public AjaxResult getLatestByDeviceId(@ApiParam("设备ID") @PathVariable("deviceId") String deviceId) {
        List<SysHealthData> list = sysHealthDataService.selectLatestByDeviceId(deviceId);
        return success(list);
    }

    /**
     * 根据用户ID查询最新健康数据
     */
    @ApiOperation("根据用户ID查询最新健康数据")
    @GetMapping("/latest/user/{userId}")
    public AjaxResult getLatestByUserId(@ApiParam("用户ID") @PathVariable("userId") Long userId) {
        List<SysHealthData> list = sysHealthDataService.selectLatestByUserId(userId);
        return success(list);
    }

    /**
     * 生成模拟的健康数据并保存到数据库，然后调用generateAlertsFromHealthData生成报警数据
     */
    @ApiOperation("生成模拟的健康数据并保存到数据库")
    @GetMapping("/generateMockHealthData")
    @Anonymous
    public AjaxResult generateMockHealthData(@ApiParam("心率数据  90 单位:次/分钟") String heartRate,
                                             @ApiParam("血压数据  95,125 收缩压,舒张压, 单位:mmHg") String bloodPressure,
                                             @ApiParam("血氧数据  95 单位:%") String bloodOxygen,
                                             @ApiParam("用户id") Long userId,
                                             @ApiParam("设备id") Long deviceId) {
        sysHealthDataService.generateMockHealthData(heartRate, bloodPressure, bloodOxygen, userId, deviceId);
        return success("成功清理 ");
    }
}